package application;

import dao.inte.DeliveryMethodRepository;
import dao.inte.PayMethodRepository;
import domain.shop.DeliveryMethod;
import domain.shop.PayMethod;
import domain.shop.Product;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Hold information about current visitors session.
 */
public class MySession implements Serializable {
    private Long userId;
    private Map<Long, Integer> basket;

    private BigDecimal totalPrice;


    @Autowired(required = true)
    private DeliveryMethodRepository deliveryMethodRepository;

    private PayMethodRepository payMethodRepository;

    private Long deliveryMethodId;
    private Long payMethodId;

    public DeliveryMethod getDeliveryMethod() {
        if (deliveryMethodId == null) {
            return null;
        }
        System.err.println(deliveryMethodRepository);
        return deliveryMethodRepository.findById(deliveryMethodId);
    }

    public void setDeliveryMethod(DeliveryMethod deliveryMethodId) {
        this.deliveryMethodId = deliveryMethodId.getId();
    }

    public Long getDeliveryMethodId() {
        return deliveryMethodId;
    }

    public void setDeliveryMethodId(Long deliveryMethodId) {
        this.deliveryMethodId = deliveryMethodId;
    }

    public Long getPayMethodId() {
        return payMethodId;
    }

    public void setPayMethodId(Long payMethodId) {
        this.payMethodId = payMethodId;
    }

    public PayMethod getPayMethod() {
        if (payMethodId == null) {
            return null;
        }
        return payMethodRepository.findById(payMethodId);
    }

    public void setPayMethod(PayMethod payMethodId) {
        this.payMethodId = payMethodId.getId();
    }

    public MySession() {
    }


    public void setPayMethod(Long payMethodId) {
        this.payMethodId = payMethodId;
    }

    public void setDeliveryMethod(Long deliveryMethodId) {
        this.deliveryMethodId = deliveryMethodId;
    }


    public Map<Long, Integer> getBasket() {
        if (basket == null) {
            return basket = new HashMap<Long, Integer>();
        }
        return basket;
    }

    /**
     * Return number of basket items in basket.
     * i.e. 2xsocks and 1xshoes = 2.
     *
     * @return
     */
    public Integer getDifferentItemCountInBasket() {
        if (basket == null) {
            return 0;
        } else {
            return basket.size();
        }
    }

    public BigDecimal getTotalPrice() {
        return totalPrice;
    }

    public String getTotalPriceAsString() {
        if (totalPrice == null) {
            return "0";
        }
        return totalPrice.toString();
    }

    public void setTotalPrice(BigDecimal totalPrice) {
        this.totalPrice = totalPrice;
    }

    public void updateBasketProduct(Product product, Integer multiplicity) {
        if (basket == null) {
            basket = new HashMap<Long, Integer>();
        }
        basket.put(product.getId(), multiplicity);
    }

    public void addProductToBasket(Product product, Integer ammount) {
        final Long id = product.getId();
        if (basket == null) {
            basket = new HashMap<Long, Integer>();
        }
        if (basket.containsKey(id)) {
            basket.put(id, basket.get(id) + ammount);
        } else {
            basket.put(id, ammount);
        }
    }

    public void removeProductFromBasket(Product product) {
        final Long id = product.getId();
        if (basket != null) {
            basket.remove(id);
        }
    }

    /**
     * Return userID of currend logged user.
     *
     * @return userID
     */
    public Long getId() {
        return userId;
    }

    public void setId(Long id) {
        this.userId = id;
    }

    /**
     * Set basket and totalPrice to null value.
     */
    public void reset() {
        basket = null;
        totalPrice = null;
    }

    @Autowired(required = true)

    public void setDeliveryMethodRepository(DeliveryMethodRepository deliveryMethodRepository) {
        this.deliveryMethodRepository = deliveryMethodRepository;
    }

    @Autowired(required = true)

    public void setPayMethodRepository(PayMethodRepository payMethodRepository) {
        this.payMethodRepository = payMethodRepository;
    }
}
